package cn.lhq.service;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.lhq.bean.User;
import cn.lhq.bean.VoteResult;
import cn.lhq.dao.ActivityDao;
import cn.lhq.dao.UserDao;
@Service
public class UserServiceImpl implements UserService{
	@Resource
	private UserDao userDao;
	@Resource
	private ActivityDao activityDao;

	public VoteResult createUser(String username,String password,Integer role) throws Exception {
		User db_user = userDao.findByName(username);
		VoteResult result = new VoteResult();
		if(db_user != null){
			result.setStatus(1);
			result.setMsg("用户已存在");
			return result;
		}
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setRole(role);
		user.setError_count(0);
		userDao.save(user);
		
		result.setStatus(0);
		result.setMsg("创建用户成功");
		return result;
	}

	public User findByName(String username) throws Exception {
		User user = userDao.findByName(username);
		return user;
	}
	
	public User findById(int id) throws Exception {
		User user = userDao.findById(id);
		return user;
	}


	public VoteResult delete(String username) throws Exception {
		VoteResult result = new VoteResult();
		User user = userDao.findByName(username);
		activityDao.deleteByCreatorId(user.getId());
		userDao.delete(username);
		result.setStatus(0);
		result.setMsg("删除用户及活动成功");
		return result;
	}
	
	public VoteResult checkLogin(String username,String password,Long timestamp){
		
		User user = userDao.findByName(username);
		
		VoteResult result = new VoteResult();
		if(user == null){
			result.setStatus(1);
			result.setMsg("用户不存在");
			return result;
		}
		
		Timestamp last_login = user.getLast_login();
		Long last_time = last_login.getTime();
		Long time_subtract = timestamp - last_time;
		Long hours = (Long)time_subtract/1000/3600;
		if(hours >24){
			user.setError_count(0);
			userDao.update(user);
		}
		
		if(user.getError_count() >= 5){
			result.setStatus(3);
			result.setMsg("错误次数过多");
			return result;
		}
		
		if(!password.equals(user.getPassword())){
			Integer error_count = user.getError_count();
			Timestamp date = new Timestamp(timestamp);
			user.setLast_login(date);
			user.setError_count(error_count+1);
			userDao.update(user);
			result.setStatus(2);
			result.setMsg("密码不正确");
			return result;
		}
		
		result.setStatus(0);
		result.setMsg("登录成功");
		result.setData(user);
		return result;
	}

	public VoteResult findAll() throws Exception {
		VoteResult result = new VoteResult();
		List<User> list = userDao.findAll();
		if(list == null){
			result.setStatus(1);
			result.setMsg("没有记录");
		}
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(list);
		return result;
	}

	public VoteResult modifyPwd(String username,String password) throws Exception {
		VoteResult result = new VoteResult();
		User user = userDao.findByName(username);
		user.setPassword(password);
		userDao.update(user);
		result.setStatus(0);
		result.setMsg("修改成功");
		return result;
	}

	public VoteResult modifyPwd2(String username, String oldPwd, String newPwd)
			throws Exception {
		VoteResult result = new VoteResult();
		User user = userDao.findByName(username);
		if(!user.getPassword().equals(oldPwd)){
			result.setStatus(1);
			result.setMsg("原始密码不正确");
		}
		user.setPassword(newPwd);
		userDao.update(user);
		result.setStatus(0);
		result.setMsg("修改成功");
		return result;
	}


}
